package cn.tedu.toa.management.sys.service.impl;

import cn.tedu.toa.management.sys.entity.SysMenus;
import cn.tedu.toa.management.sys.mapper.SysMenusMapper;
import cn.tedu.toa.management.sys.service.SysMenusService;
import cn.tedu.toa.management.sys.service.ex.*;
import cn.tedu.toa.management.sys.vo.MenuNodeVo;
import cn.tedu.toa.management.sys.vo.MenuVo;
import cn.tedu.toa.management.util.UserUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 资源管理 服务实现类
 * </p>
 *
 * @author huaqian
 * @since 2021-09-09
 */
@Service
public class SysMenusServiceImpl extends ServiceImpl<SysMenusMapper, SysMenus> implements SysMenusService {
    @Autowired
    private SysMenusMapper menusMapper;

    @Override
    @RequiresPermissions("sys:menu:view")
    public List<MenuVo> listMenuVo() {
        List<MenuVo> list=menusMapper.selectMenuList();
        //list=null;
        if(list==null || list.size()==0){
            //抛出业务异常 -- RecordNotFoundException
            throw new RecordNotFoundException("菜单列表查询异常!");
        }
        return list;
    }

    @Override
    public void removeMenuById(Integer menuId) {
        //先判断参数是否合法
        if(menuId==null|| menuId<1){
            throw new ParamIllegelException("删除菜单异常!菜单id不合法!");
        }
        //判断菜单是否真实存在
         SysMenus menus=menusMapper.selectById(menuId);
        if(menus==null) {
            throw new RecordNotFoundException("删除菜单异常!该菜单不存在,请重新选择!");
        }
        //判断是否有子菜单
        Integer count=menusMapper.selectSubMenuCountByMenuId(menuId);
        if(count>0){
            throw  new IllegalOperationException("删除菜单失败!该菜单有子菜单,不能这样!");
        }
        //查询菜单被角色占用
        count=menusMapper.selectRoleMenuCoutnByMenuId(menuId);
        if(count>0){
            throw new IllegalOperationException("删除菜单失败!该菜单正在被角色使用,无法删除");
        }
        //删除菜单
        count=menusMapper.deleteById(menuId);
        if(count!=1){
            throw new DeleteException("删除菜单异常!");
        }
    }

    @Override
    public List<MenuNodeVo> findMenuNodes() {
        List<MenuNodeVo> list= menusMapper.selectMenuNodes();
      //  list=null;
        if(list==null || list.size()==0){
            throw new ParentNodeNotFoundException("父级菜单数据查询异常!");
        }
        return list;
    }

    @Override
    public void saveMenu(SysMenus sysMenus) {
        //判断菜单是否为null
        if(sysMenus==null){
            throw  new ParamIllegelException("添加菜单异常,菜单数据为空!");
        }
        //判断父级菜单是否存在
        SysMenus parentMenu=menusMapper.selectById(sysMenus.getParentId());
        if(parentMenu==null){
            throw new ParentNodeNotFoundException("添加菜单异常,父级菜单不存在!");
        }
        //不全数据
        sysMenus.setCreatedUser(UserUtils.getCurrentUserName())
        .setModifiedUser(UserUtils.getCurrentUserName())
                .setCreatedTime(LocalDateTime.now())
        .setModifiedTime(LocalDateTime.now());
        //插入数据
        int row=menusMapper.insert(sysMenus);
        if(row==0){
            throw new InsertException("添加菜单失败,请联系管理员!");
        }

    }

    /**
     * 查询MenuVo 用于修改回显
     * @param id
     * @return
     */
    @Override
    public MenuVo selectMenuVo(Integer id) {
        if(id<=0 || id==null){
            throw new ParamIllegelException("修改菜单异常,菜单id不合法!");
        }
        MenuVo menuVo=menusMapper.selectMenuVo(id);
        if(menuVo==null){
            throw new RecordNotFoundException("修改菜单异常!该记录不存在!");
        }
        return menuVo;
    }

    @Override
    public void modifyMenuById(SysMenus sysMenus) {
        //判断参数是否为空
        if(sysMenus==null){
            throw new ParamIllegelException("修改菜单数据异常,菜单数据为空!");
        }
        //修改操作
        sysMenus.setModifiedUser(UserUtils.getCurrentUserName());
        sysMenus.setModifiedTime(LocalDateTime.now());
        //修改持久层进行更新
        int row=menusMapper.updateById(sysMenus);
        //对返回结果进行不为1 判断
        if(row!=1){
            throw new UpdateException("修改菜单失败!请联系管理员!");
        }

    }

}
